package com.medicine.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import pagination.Page;
import po.Equipment;
import po.Ypkcxx;
import requestvo.EquipmentReqVo;
@Controller
public class EquipmentController extends BaseController{
	
	@RequestMapping("/sbkcxx")
	public void findsbkcxx(EquipmentReqVo equipmentReqVo,HttpServletResponse response) throws IOException{
		Page<EquipmentReqVo> page=sbkcxxService.findSbkcxx(equipmentReqVo);
		JSONObject jsonObject=new JSONObject();
		jsonObject.element("rows", page.getResults());
		jsonObject.element("total", page.getTotalRecord());
		response.setCharacterEncoding("UTF-8");  
		response.setContentType("application/json; charset=utf-8");  
		response.getWriter().print(jsonObject.toString());		
	}
	@RequestMapping("/updatesbkcxx")
	public void updateYpkcxx(EquipmentReqVo equipmentReqVo,HttpServletResponse response) throws IOException{
		System.out.println(equipmentReqVo);
		sbkcxxService.updatesbkcxx(equipmentReqVo);
		response.sendRedirect("sbkcxx.html");
	}
	@RequestMapping("/addsbkcxx")
	public void addYpkcxx(EquipmentReqVo equipmentReqVo,HttpServletResponse response,HttpServletRequest request) throws IOException{	
		/*判断是否存在该sb_no的设备*/
		System.out.println(equipmentReqVo);
		String sb_no = request.getParameter("sb_no");
		EquipmentReqVo equipmentReqVoTemp=sbkcxxService.getEquipmentNo(sb_no);
		try {
			if(equipmentReqVoTemp==null||equipmentReqVoTemp.equals("")){//表示不存在该设备no
			//	request.setAttribute("sb_no", "-1");
				sbkcxxService.addsbkcxx(equipmentReqVo);
				response.sendRedirect("sbkcxx.html");				
				//return "500.jsp";
			}else {				
				response.setContentType("text/html;charset=UTF-8");
				PrintWriter out = response.getWriter();
				int code=-2;
				out.print(code);// 返回信息
			//	response.getWriter().print(code);
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/delsbkcxx")
	public void delsbkcxx(String ids,HttpServletResponse response) throws IOException{
		String[] id=ids.split(",");
		String[] integers=new String[id.length];
		for(int i=0;i<id.length;++i)
			integers[i]=id[i];
		sbkcxxService.delsbkcxx(integers);
		response.sendRedirect("sbkcxx.html");
	}
	
 /*	@RequestMapping("/checksbno")
	public void checkYPBM(Integer yPBM,HttpServletResponse response) throws IOException{
		Ypkcxx ypxx= ypkcxxService.getYpkcxx(yPBM);
		if(ypxx==null)
			response.getWriter().print("null");
		else
			response.getWriter().print(ypxx.getyPMC());
	}*/
	/*
	@RequestMapping("/findAllYpxx")
	public void findAllYpxx(HttpServletResponse response) throws IOException{
		
		response.setCharacterEncoding("UTF-8");  
		response.setContentType("application/json; charset=utf-8"); 
		JsonObject jsonObject=new JsonObject();
		JSONArray jsonArray=JSONArray.fromObject(ypkcxxService.findAll());
		response.getWriter().print(jsonArray.toString());
	}
	*/
}
